package com.ruoyi.btlfbreed.service.impl;

import java.util.List;

import com.ruoyi.btlfbreed.domain.MyTbChickenGrowData;
import com.ruoyi.btlfbreed.domain.MyTbChickenHatchRecordsData;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.btlfbreed.domain.TbChickens;
import com.ruoyi.btlfbreed.mapper.TbChickenHatchRecordsMapper;
import com.ruoyi.btlfbreed.domain.TbChickenHatchRecords;
import com.ruoyi.btlfbreed.service.ITbChickenHatchRecordsService;

/**
 * 肉鸡孵化记录Service业务层处理
 * 
 * @author cpdeman
 * @date 2024-05-05
 */
@Service
public class TbChickenHatchRecordsServiceImpl implements ITbChickenHatchRecordsService 
{
    @Autowired
    private TbChickenHatchRecordsMapper tbChickenHatchRecordsMapper;

    /**
     * 查询肉鸡孵化记录
     * 
     * @param id 肉鸡孵化记录主键
     * @return 肉鸡孵化记录
     */
    @Override
    public TbChickenHatchRecords selectTbChickenHatchRecordsById(Long id)
    {
        return tbChickenHatchRecordsMapper.selectTbChickenHatchRecordsById(id);
    }

    /**
     * 查询肉鸡孵化记录列表
     * 
     * @param tbChickenHatchRecords 肉鸡孵化记录
     * @return 肉鸡孵化记录
     */
    @Override
    public List<TbChickenHatchRecords> selectTbChickenHatchRecordsList(TbChickenHatchRecords tbChickenHatchRecords)
    {
        return tbChickenHatchRecordsMapper.selectTbChickenHatchRecordsList(tbChickenHatchRecords);
    }

    /**
     * 新增肉鸡孵化记录
     * 
     * @param tbChickenHatchRecords 肉鸡孵化记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbChickenHatchRecords(TbChickenHatchRecords tbChickenHatchRecords)
    {
        tbChickenHatchRecords.setCreateTime(DateUtils.getNowDate());
        int rows = tbChickenHatchRecordsMapper.insertTbChickenHatchRecords(tbChickenHatchRecords);
        insertTbChickens(tbChickenHatchRecords);
        return rows;
    }

    /**
     * 修改肉鸡孵化记录
     * 
     * @param tbChickenHatchRecords 肉鸡孵化记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbChickenHatchRecords(TbChickenHatchRecords tbChickenHatchRecords)
    {
        tbChickenHatchRecords.setUpdateTime(DateUtils.getNowDate());
        tbChickenHatchRecordsMapper.deleteTbChickensByParentId(tbChickenHatchRecords.getId());
        insertTbChickens(tbChickenHatchRecords);
        return tbChickenHatchRecordsMapper.updateTbChickenHatchRecords(tbChickenHatchRecords);
    }

    /**
     * 批量删除肉鸡孵化记录
     * 
     * @param ids 需要删除的肉鸡孵化记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbChickenHatchRecordsByIds(Long[] ids)
    {
        tbChickenHatchRecordsMapper.deleteTbChickensByParentIds(ids);
        return tbChickenHatchRecordsMapper.deleteTbChickenHatchRecordsByIds(ids);
    }

    /**
     * 删除肉鸡孵化记录信息
     * 
     * @param id 肉鸡孵化记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbChickenHatchRecordsById(Long id)
    {
        tbChickenHatchRecordsMapper.deleteTbChickensByParentId(id);
        return tbChickenHatchRecordsMapper.deleteTbChickenHatchRecordsById(id);
    }

    @Override
    public List<MyTbChickenHatchRecordsData> getYearHatchData()
    {
        return tbChickenHatchRecordsMapper.getYearHatchData();
    }

    /**
     * 新增肉鸡信息
     * 
     * @param tbChickenHatchRecords 肉鸡孵化记录对象
     */
    public void insertTbChickens(TbChickenHatchRecords tbChickenHatchRecords)
    {
        List<TbChickens> tbChickensList = tbChickenHatchRecords.getTbChickensList();
        Long id = tbChickenHatchRecords.getId();
        if (StringUtils.isNotNull(tbChickensList))
        {
            List<TbChickens> list = new ArrayList<TbChickens>();
            for (TbChickens tbChickens : tbChickensList)
            {
                tbChickens.setParentId(id);
                tbChickens.setIsSold("N");
                list.add(tbChickens);
            }
            if (list.size() > 0)
            {
                tbChickenHatchRecordsMapper.batchTbChickens(list);
            }
        }
    }
}
